let logger = require("pomelo-logger").getLogger(__filename);
const CONFIG_COLLECTION_NAME = "configs";

let Dao = function (app) {
    this.app = app;
};

Dao.prototype.findByKey = function (key, callback) {
    let dbClient = this.app.get("gameDBClient");
    dbClient.collection(CONFIG_COLLECTION_NAME, function (error, collection) {
        if(!!error) {
            logger.error(`find config failed. get collection error. error=${error}`);
            callback(null);
            return;
        }
        collection.findOne({key : key}, function (error, document) {
            if(!!error) {
                logger.error(`findByKey in configs failed. error=${error}`);
                callback(null);
                return;
            }
            callback(document);
        });
    });
};

Dao.prototype.findAllConfigs = function (callback) {
    let dbClient = this.app.get("gameDBClient");
    dbClient.collection(CONFIG_COLLECTION_NAME, function (error, collection) {
        if(!!error) {
            logger.error(`findAllConfigs in configs failed. get collection error. error=${error}`);
            callback([]);
        }
        collection.find({}).toArray(function (error, documents) {
            if(error){
                logger.error(`findAllConfigs in configs failed. error=${error}`);
                callback([]);
                return;
            }
            callback(documents);
        });
    });
};

Dao.prototype.insertOne = function (document, callback) {
    let dbClient = this.app.get("gameDBClient");
    dbClient.collection(CONFIG_COLLECTION_NAME, function (error, collection) {
        if(!!error) {
            logger.error(`insert config in configs failed. get collection error. error=${error}`);
            callback(null);
            return;
        }
        collection.insertOne(document, function (error, result) {
            if (!!error) {
                logger.error(`insert config in configs failed. error=${error}`);
                callback(null);
                return;
            }
            callback(result);
        });
    });
};

Dao.prototype.upsertValue = function (key, value, callback) {
    let dbClient = this.app.get("gameDBClient");
    dbClient.collection(CONFIG_COLLECTION_NAME, function (error, collection) {
        if(!!error) {
            logger.error(`update config in configs failed. get collection error. error=${error}`);
            callback(null);
            return;
        }
        collection.updateOne(
            {key : key},
            {$set:{value : value}},
            {upsert : true},
            function (error, result) {
                if (!!error) {
                    logger.error(`update config in configs failed. error=${error}`);
                    callback(null);
                    return;
                }
                callback(result);
        });
    });
};

Dao.prototype.updateValue = function (key, value, callback) {
    let dbClient = this.app.get("gameDBClient");
    dbClient.collection(CONFIG_COLLECTION_NAME, function (error, collection) {
        if(!!error) {
            logger.error(`update config in configs failed. get collection error. error=${error}`);
            callback(null);
            return;
        }
        collection.updateOne(
            {key : key},
            {$set:{value : value}},
            function (error, result) {
                if (!!error) {
                    logger.error(`update config in configs failed. error=${error}`);
                    callback(null);
                    return;
                }
                callback(result);
            });
    });
};

Dao.prototype.updateEnable = function (key, enable, callback) {
    let dbClient = this.app.get("gameDBClient");
    dbClient.collection(CONFIG_COLLECTION_NAME, function (error, collection) {
        if(!!error) {
            logger.error(`update config in configs failed. get collection error. error=${error}`);
            callback(null);
            return;
        }
        collection.updateOne(
            {key : key},
            {$set:{enable : enable}},
            function (error, result) {
                if (!!error) {
                    logger.error(`update config in configs failed. error=${error}`);
                    callback(null);
                    return;
                }
                callback(result);
            });
    });
};

Dao.prototype.deleteByKey = function (key, callback) {
    let dbClient = this.app.get("gameDBClient");
    dbClient.collection(CONFIG_COLLECTION_NAME, function (error, collection) {
        if(!!error) {
            logger.error(`delete config in configs failed. get collection error. error=${error}`);
            callback(null);
            return;
        }
        collection.findOneAndDelete({key : key}, function (error, result) {
            if (!!error) {
                logger.error(`delete config in configs failed. error=${error}`);
                callback(null);
                return;
            }
            callback(result);
        });
    });
};


module.exports.create = function (app) {
    return new Dao(app);
};
